package s;

import java.io.FileInputStream;
import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.PrintWriter;
import java.sql.Connection;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.Properties;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.SAXException;

import com.aetrion.flickr.Flickr;
import com.aetrion.flickr.FlickrException;
import com.aetrion.flickr.REST;
import com.aetrion.flickr.photos.Photo;
import com.aetrion.flickr.photos.PhotosInterface;
import com.aetrion.flickr.tags.Tag;
import java.sql.Statement;


public class GetImageTags extends HttpServlet{

		private static final long serialVersionUID = 1L;
		private Properties props;
		private String propsPath;
	
		@Override
		public void init(ServletConfig config) throws ServletException {
			super.init(config);
			
			this.propsPath = this.getServletContext().getRealPath("/");
			this.props = new Properties();
			try {
				props.loadFromXML(new FileInputStream(propsPath + "properties.properties"));
			} catch (FileNotFoundException e) {e.printStackTrace();} 
			catch (IOException e) {e.printStackTrace();}
	}
	
		@SuppressWarnings("unchecked")
		@Override
		protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
			
				String imageURI = req.getParameter("imageURI");
			
				if(imageURI.indexOf("local://") == 0){
					
						String filename = imageURI.substring(imageURI.lastIndexOf("/")+1, imageURI.length());
						String response = "<tags>";
						Connection connection = null;
						try {
							Class.forName("com.mysql.jdbc.Driver");
							connection = DriverManager.getConnection(props.getProperty("DB_URL"), props.getProperty("DB_USER"), props.getProperty("DB_PASS"));
								Statement statement = (Statement) connection.createStatement();
								ResultSet rs = statement.executeQuery("SELECT tag.Tag as x FROM tag, imgTag WHERE tag.IdTag = imgTag.IdTag AND imgTag.IdImg = (SELECT img.IdImg FROM img WHERE img.Filename ='"+ filename +"');"); 
								while(rs.next()){
										response = response.concat("<tag>"+rs.getString("x")+"</tag>");
								}
								response = response.concat("</tags>");
						}
						catch (SQLException e) {e.printStackTrace();} catch (ClassNotFoundException e) {
							e.printStackTrace();
						}
						finally {
								try {connection.close();} 
								catch (SQLException e) {e.printStackTrace();}
						}
						
						PrintWriter pw = res.getWriter();
						pw.write(response);
					
				}
				else if(imageURI.indexOf("http://") == 0){
					
						String id = imageURI.substring(imageURI.lastIndexOf("/")+1, imageURI.lastIndexOf("_"));
						String response = "<tags>";
						REST rest;
						try {
								rest = new REST();
								rest.setHost(props.getProperty("FLICKR_URL"));
								Flickr flickr=new Flickr(props.getProperty("FLICKR_KEY"),rest);
							    Flickr.debugStream=false;
							    PhotosInterface photosInterface=flickr.getPhotosInterface();
							    Photo photo = photosInterface.getPhoto(id);
							    Iterator<Tag> it = photo.getTags().iterator();
							    while(it.hasNext()){
								    	Tag t = it.next();
								    	response = response.concat("<tag>"+t.getValue()+"</tag>");
							    }
						} 
						catch (ParserConfigurationException e) {e.printStackTrace();} catch (FlickrException e) {e.printStackTrace();} 
						catch (SAXException e) {e.printStackTrace();}
						
						response = response.concat("</tags>");
						
						PrintWriter pw = res.getWriter();
						pw.write(response);
				
				}
				else{
						PrintWriter pw = res.getWriter();
						pw.write("FAIL");
						return;
				}
				
		}
}